package com.home.school.controller;

import com.home.school.bean.TSubject;
import com.home.school.bean.TTeacherSubject;
import com.home.school.service.TSubjectService;
import com.home.school.service.TTeacherSubjectService;
import com.home.school.service.TUserService;
import com.home.school.util.SnowflakeIdUtil;
import com.home.school.vo.SubjectListVO;
import com.home.school.vo.SubjectVO;
import com.home.school.model.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/api/subject")
@RestController
public class SubjectController {

    @Autowired
    private TSubjectService tSubjectService;

    @Autowired
    private TTeacherSubjectService tTeacherSubjectService;

    @Autowired
    private TUserService tUserService;

    /**
     * 保存课程
     * @param tSubject
     * @return
     */
    @RequestMapping("/save")
    public R save(@RequestBody SubjectVO tSubject){
        TSubject transfer = tSubject.transfer();
        String snowflakeId = SnowflakeIdUtil.getSnowflakeId();
        transfer.setNum(snowflakeId);
        tSubjectService.save(transfer);
        TTeacherSubject teacherSubject = new TTeacherSubject();
        teacherSubject.setSujectId(snowflakeId);
        teacherSubject.setTeaId(String.valueOf(tSubject.getTeacherId()));
        tTeacherSubjectService.save(teacherSubject);
        return R.ok();
    }
    /**
     * 更新课程
     * @param tSubject
     * @return
     */
    @RequestMapping("/update")
    public R register(@RequestBody TSubject tSubject){
        tSubjectService.updateById(tSubject);
        return R.ok();
    }

    /**
     * 课程id查询
     * @param id
     * @return
     */
    @RequestMapping("byId/{id}")
    public R byId(@PathVariable("id") String id){
        return R.ok().data(tSubjectService.getById(id));
    }

    /**
     * 课程 num 查询
     * @param num
     * @return
     */
    @RequestMapping("byNum/{num}")
    public R byNum(@PathVariable("num") String num){
        TTeacherSubject bySubjectId = tTeacherSubjectService.findBySubjectId(num);
        String teaId = bySubjectId.getTeaId();
        return R.ok().data(tUserService.getById(teaId));
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("deleted/{id}")
    public R deleted(@PathVariable("id") Integer id){
        TSubject subject = tSubjectService.getById(id);
        String num = subject.getNum();
        tTeacherSubjectService.deletedBySubjectId(num);
        return R.ok().data(tSubjectService.removeById(id));
    }

    /**
     * 列表查询 课程
     * @param userListVO
     * @return
     */
    @RequestMapping("getList")
    public R getListUrl(@RequestBody SubjectListVO userListVO){
        return tSubjectService.pageList(userListVO);
    }

    /**
     * 教师查询 课程列表
     * @param userListVO
     * @return
     */
    @RequestMapping("getTeacherList")
    public R getTeacherList(@RequestBody SubjectListVO userListVO){
        return tSubjectService.getTeacherList(userListVO);
    }




}
